package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduModuleDao;
import com.stormrage.edu.dao.edu.view.EduMajorModuleDao;
import com.stormrage.edu.dao.edu.view.EduModuleCourseDao;
import com.stormrage.edu.serv.edu.EduModuleServ;
import com.stormrage.edu.serv.edu.view.EduModuleCourseServ;
import com.stormrage.entity.edu.EduModule;
import com.stormrage.entity.edu.rep.EduModuleRep;
import com.stormrage.entity.edu.req.EduModuleReq;
import com.stormrage.entity.exam.rep.ExmCountRep;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormrage.entity.learn.req.ExmTermNessReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class EduModuleServImpl implements EduModuleServ {

	private static final Logger logger = LoggerFactory.getLogger(EduModuleServImpl.class);

	@Autowired
	private EduModuleDao eduModuleDao;

	@Autowired
	private EduMajorModuleDao eduMajorModuleDao;

	@Autowired
	private EduModuleCourseDao eduModuleCourseDao;

	@Autowired
	private EduModuleCourseServ eduModuleCourseServ;

	@Override
	public Integer create(final EduModule arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduModuleDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduModule arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Update 】- 开始运行 ");

		eduModuleDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduModuleDao.delete(map);
		eduMajorModuleDao.deleteByModuleId(map);
		eduModuleCourseDao.deleteByModuleId(map);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final EduModuleReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("module", arg0.getModule());

		Integer index = eduModuleDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduModule queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduModule entity = eduModuleDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduModuleRep> queryListForExam(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduModule> array = eduModuleDao.queryListById(map);

		List<EduModuleRep> list = new ArrayList<EduModuleRep>();
		int seq = 1;

		Iterator<EduModule> it = array.iterator();
		EduModuleRep entityRep = null;
		while (it.hasNext()) {
			EduModule module = it.next();
			Integer courCount = eduModuleCourseServ.queryCountById(module.getId());
			entityRep = EduModuleRep.instance(module, seq, courCount);
			list.add(entityRep);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 完成运行 ");
		return list;
	}

	@Override
	public ListRep queryList(final EduModuleReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduModuleDao.queryCount(map);

		List<EduModule> array = eduModuleDao.queryList(map);

		List<EduModuleRep> list = new ArrayList<EduModuleRep>();
		int seq = 1;

		Iterator<EduModule> it = array.iterator();
		EduModuleRep entityRep = null;
		while (it.hasNext()) {
			EduModule module = it.next();
			Integer courCount = eduModuleCourseServ.queryCountById(module.getId());
			entityRep = EduModuleRep.instance(module, seq, courCount);
			list.add(entityRep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public ListRep queryList(final Integer arg0, final EduModuleReq arg1) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduModule> array = eduModuleDao.queryList();
		array.removeAll(queryListById(arg0));

		Iterator<EduModule> it = array.iterator();
		while (it.hasNext()) {
			EduModule mod = it.next();
			if (null != arg1.getModule() && !arg1.getModule().equals("") && !mod.getModule().contains(arg1.getModule()))
				it.remove();
		}

		List<EduModuleRep> list = new ArrayList<EduModuleRep>();
		int seq = arg1.getPage().getStartPage() + 1;

		for (int i = 0; i < array.size(); i++) {
			if (i >= arg1.getPage().getStartPage() && i <= arg1.getPage().getEndPage()) {
				EduModule mod = array.get(i);
				Integer count = eduModuleCourseServ.queryCountById(mod.getId());
				EduModuleRep modRep = EduModuleRep.instance(mod, seq, count);
				list.add(modRep);
				seq++;
			}
		}

		ListRep rep = ListRep.instance(array.size(), list);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduModule> queryListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduModule> array = eduModuleDao.queryListById(map);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduModule> queryRepListByTerm(final ExmTermNessReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 queryRepListByTermNess 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getTermId());
		map.put("ness", arg0.getNess());

		List<EduModule> array = eduModuleDao.queryRepListByTerm(map);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 queryRepListByTermNess 】- 完成运行 ");
		return array;
	}

	@Override
	public ExmCountRep queryRepListByTermNess(final ExmTermNessReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 queryRepListByTermNess 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getTermId());
		map.put("ness", arg0.getNess());

		List<ExmExamCountRep> array = eduModuleDao.queryRepListByTermNess(map);
		int seq = 1;
		int sele = 0, mult = 0, corr = 0, total = 0;

		Iterator<ExmExamCountRep> it = array.iterator();
		while (it.hasNext()) {
			ExmExamCountRep entity = it.next();
			entity = ExmExamCountRep.instance(entity, seq);
			sele += entity.getSele();
			mult += entity.getMult();
			corr += entity.getCorr();
			seq++;
		}
		total = sele + mult + corr;
		
		ExmCountRep rep = ExmCountRep.instance(sele, mult, corr, total, array);

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 queryRepListByTermNess 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduModuleRep> queryRepListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryRepListById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduModuleRep> array = eduModuleDao.queryRepListById(map);
		int seq = 1;

		Iterator<EduModuleRep> it = array.iterator();
		while (it.hasNext()) {
			EduModuleRep modRep = it.next();
			Integer modCount = eduModuleCourseServ.queryCountById(modRep.getId());
			modRep = EduModuleRep.instance(modRep, seq, modCount);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduModuleServ 】- 【 QueryRepListById 】- 完成运行 ");
		return array;
	}

	private Map<String, Object> initQuery(final EduModuleReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getModule() && arg0.getModule().length() > 0) {
			query.append("module LIKE :module AND ");
			map.put("module", "%" + arg0.getModule() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
